﻿

var DDL_OverOptionCss = "background: highlight; color: highlighttext";
var DDL_SizedBorderCss = "2 inset buttonhighlight";
var DDL_GlobalSelect;	
var q = 0;

function DDL_SelectDefault(eventTarget,SkinName)
{
	eventTarget.src = "../../Images/DropDownList/SelectBox/" + SkinName + "/SButton.gif";
}

function DDL_SelectDown(eventTarget,SkinName)
{
	if(event.button==1)
	{
		eventTarget.src = "../../Images/DropDownList/SelectBox/"+SkinName+"/SDButton.gif";
		DDL_ToggleDropDown(eventTarget.parentElement.parentElement.parentElement.parentElement);
	}
}

function DDL_InitSelectBox(el)
{
	var selectedIndex = DDL_FindSelected(el);

	if(el.children[2].value == "")
	{
		if(el.children[1].children.length != 0)
		{
			selectedCell = el.children[0].rows[0].cells[0];
			selectedDiv  = 	el.children[1].children[0];
			selectedCell.innerHTML = selectedDiv.innerHTML;
			el.children[2].value = 0;
		}
	}
	else
	{
		selectedCell = el.children[0].rows[0].cells[0];
		selectedDiv  = 	el.children[1].children[el.children[2].value];
		selectedCell.innerHTML = selectedDiv.innerHTML;
	}

	var size = el.getAttribute("size");
	el.options = el.children[1].children;
	el.selectedIndex = selectedIndex;	//Set the index now!
	dropdown = el.children[1];

	if (size != null)
	{
		if (size > 1)
		{
			el.size = size;
			dropdown.style.zIndex = 0;
			DDL_InitSized(el);
		}
		else
		{
			el.size = 1;
			dropdown.style.zIndex = 99;
			if (dropdown.offsetHeight > 146)
			{
				dropdown.style.height = "146";
				dropdown.style.overflow = "auto";
			}
		}
	}
	DDL_HighlightSelected(el,true);
}

function DDL_InitSized(el)
{
	var h = 0;
	el.children[0].style.display = "none";

	dropdown = el.children[1];
	dropdown.style.visibility = "visible";

	if (dropdown.children.length > el.size)
	{
		dropdown.style.overflow = "auto";
		for (var i=0; i<el.size; i++)
		{
			h += dropdown.children[i].offsetHeight;
		}

		if (dropdown.style.borderWidth != null)
		{
			dropdown.style.pixelHeight = h + 4; 
		}
		else
		{
			dropdown.style.height = h;
		}
	}

	dropdown.style.border = DDL_SizedBorderCss;
	el.style.height = dropdown.style.pixelHeight;
}

function DDL_CopySelected(el)
{
	var selectedIndex = DDL_FindSelected(el);
	if(selectedIndex != "-1")
	{
		selectedCell = el.children[0].rows[0].cells[0];
		selectedDiv  = 	el.children[1].children[selectedIndex];
		selectedCell.innerHTML = selectedDiv.innerHTML;
	}
}

function DDL_FindSelected(el)
{
	var selected = null;
	var ecl = 0;

	if(el.children[1] != null)
	{
		ec = el.children[1].children;	//the table is the first child
		ecl = ec.length;
	}
	


	
	for (var i=0; i<ecl; i++)
	{
		if (ec[i].getAttribute("selected") != null)
		{
			if (selected == null)	// Found first selected
			{
				selected = i;
			}
			else
			{
				ec[i].removeAttribute("selected");	//Like I said. Only one selected!
			}
		}
	}

	if (selected == null)
	{
		selected = -1;	
	}
		
	return selected;
}

function DDL_FindOptionSelected(el)
{
	var selected = null;
	ec = el.children[1].children;	
	var ecl = ec.length;
	
	for (var i=0; i<ecl; i++)
	{
		if (ec[i].getAttribute("selected") != null)
		{
			if (selected == null)	
			{
				selected = i;
			}
			else
			{
				ec[i].removeAttribute("selected");	
			}
		}
	}

	if (selected == null)
	{
		selected = 0;	
	}
		
	return selected;
}


function DDL_ToggleDropDown(el)
{
	if (el.size == 1) {
		dropDown = el.children[1];
		
		if (dropDown.style.visibility == "")
			dropDown.style.visibility = "hidden";
			
		if (dropDown.style.visibility == "hidden")
			DDL_ShowDropDown(dropDown);
		else
			DDL_HideDropDown(dropDown);
	}
}

 

function DDL_OptionClick() {
	el = DDL_GetReal(window.event.srcElement, "className", "DDL_Option");

	if (el.className == "DDL_Option") {
		dropdown  = el.parentElement;
		selectBox = dropdown.parentElement;
		oldSelected = dropdown.children[DDL_FindOptionSelected(selectBox)]

			oldSelected.removeAttribute("selected");
			el.setAttribute("selected", 1);
			selectBox.selectedIndex = DDL_FindSelected(selectBox);

		if (selectBox.onchange != null) {
			if (selectBox.id != "") {
				eval(selectBox.onchange.replace(/this/g, selectBox.id));
			}
			else {
				DDL_GlobalSelect = selectBox;
				eval(selectBox.onchange.replace(/this/g, "DDL_GlobalSelect"));
			}
		}
		
		if (el.backupCss != null)
			el.style.cssText = el.backupCss;
		DDL_CopySelected(selectBox);
		DDL_ToggleDropDown(selectBox);
		DDL_HighlightSelected(selectBox, true);
	}
}

function DDL_OptionOver() {
	var toEl = DDL_GetReal(window.event.toElement, "className", "DDL_Option");
	var fromEl = DDL_GetReal(window.event.fromElement, "className", "DDL_Option");
	if (toEl == fromEl) return;
	var el = toEl;
	
	if (el.className == "DDL_Option") {
		if (el.backupCss == null)
			el.backupCss = el.style.cssText;
		DDL_HighlightSelected(el.parentElement.parentElement, false);
		el.style.cssText = el.backupCss + "; " + DDL_OverOptionCss;
		this.highlighted = true;
	}
}

function DDL_OptionOut() {
	var toEl = DDL_GetReal(window.event.toElement, "className", "DDL_Option");
	var fromEl = DDL_GetReal(window.event.fromElement, "className", "DDL_Option");

	if (fromEl == fromEl.parentElement.children[DDL_FindSelected(fromEl.parentElement.parentElement)]) {
		if (toEl == null)
			return;
		if (toEl.className != "DDL_Option")
			return;
	}
	
	if (toEl != null) {
		if (toEl.className != "DDL_Option") {
			if (fromEl.className == "DDL_Option")
				DDL_HighlightSelected(fromEl.parentElement.parentElement, true);
		}
	}
	
	if (toEl == fromEl) return;
	var el = fromEl;

	if (el.className == "DDL_Option") {
		if (el.backupCss != null)
			el.style.cssText = el.backupCss;
	}

}

function DDL_HighlightSelected(el,add)
{
	var selectedIndex = DDL_FindSelected(el);
	if(selectedIndex != "-1")
	{
		selected = el.children[1].children[selectedIndex];
		if (add) {
			if (selected.backupCss == null)
			{
				selected.backupCss = selected.style.cssText;
			}
			selected.style.cssText = selected.backupCss + "; " + DDL_OverOptionCss;
		}
		else if (!add)
		{
			if (selected.backupCss != null)
			{
				selected.style.cssText = selected.backupCss;
			}
		}
	}
}

function DDL_HideShownDropDowns() {
	var el = DDL_GetReal(window.event.srcElement, "className", "DDL_Select");
	
	var spans = document.all.tags("SPAN");
	var selects = new Array();
	var index = 0;
	
	for (var i=0; i<spans.length; i++) {
		if ((spans[i].className == "DDL_Select") && (spans[i] != el)) {
			dropdown = spans[i].children[1];
			if ((spans[i].size == 1) && (dropdown.style.visibility == "visible"))
				selects[index++] = dropdown;
		}
	}
	
	for (var j=0; j<selects.length; j++) {
		DDL_HideDropDown(selects[j]);
	}	

}

function DDL_HideDropDown(el) {
	if (typeof(fade) == "function")
		fade(el, false);
	else
		el.style.visibility = "hidden";
}

function DDL_ShowDropDown(el) {
	if (typeof(fade) == "function")
		fade(el, true);
	else if (typeof(swipe) == "function")
		swipe(el, 2);
	else
		el.style.visibility = "visible";
}

function DDL_InitSelectBoxes() {
	var spans = document.all.tags("SPAN");
	var selects = new Array();
	var index = 0;
	
	for (var i=0; i<spans.length; i++) {
		if (spans[i].className == "DDL_Select")
			selects[index++] = spans[i];
	}
	
	for (var j=0; j<selects.length; j++) {
		DDL_InitSelectBox(selects[j]);
	}	
}

function DDL_GetReal(el, type, value) {
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return el;
}

function DDL_SelectItemEvent(eventTarget)
{
	eventTarget.children[2].value=DDL_FindOptionSelected(eventTarget)
}


function FastClick(eventTarget)
{
	eventTarget.children[2].value=DDL_FindOptionSelected(eventTarget)
}

function FastOver(eventTarget)
{
	if (eventTarget.backupCss == null)
	{
		eventTarget.backupCss = eventTarget.style.cssText;
	}
	eventTarget.style.cssText = eventTarget.backupCss + "; " + DDL_OverOptionCss;
}

function onMouseOut(eventTarget)
{
	if (eventTarget.backupCss != null)
	{
		eventTarget.style.cssText = eventTarget.backupCss;
	}
}


window.onload = DDL_InitSelectBoxes;
document.onclick = DDL_HideShownDropDowns;

